package cn.edu.nju.newsFeed;

import cn.edu.nju.R;
import cn.edu.nju.notification.NotificationActivity;
import cn.edu.nju.notification.NotificationProvider;
import cn.edu.nju.setting.SettingActivity;
import cn.edu.nju.storage.StoreDataProvider;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class NewsFeedActivity extends Activity {
	public static final String FEED_ITEM_POSITION = "feed_item_position";
	private final int INIT_LIST = 0;
	private final int UPDATE_LIST = 1;
	private TextView userNameText;
	private ListView newsFeedList;
	private View loadSection;
	private NewsFeedListAdapter listAdapter;
	private NewsFeedProvider provider;
	private Handler loadHandler;
	private Thread loadThread;
	private int loadMode;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_feed);
        
        this.provider = NewsFeedProvider.getInstance(this);
        
        // init widget
        userNameText = (TextView)findViewById(R.id.user_name_text);
        userNameText.setText(StoreDataProvider.getInstance(this).getUserName());
        loadSection = (View)findViewById(R.id.load_section);
        initLoadHandler();
        initLoadThread();
        initRefreshButton();
        initWritePostButton();
        initNewsFeedList();
    }
	
	@Override
	public void onResume(){
		super.onResume();
		if(listAdapter != null){
			listAdapter.updateInvalidView();
		}
	}
	
	public void initLoadHandler(){
		loadHandler = new Handler(){
			@Override
			public void handleMessage(Message msg){
				switch (msg.what) {
				case INIT_LIST:
					newsFeedList = (ListView)findViewById(R.id.news_feed_list);
					listAdapter = new NewsFeedListAdapter(NewsFeedActivity.this);
					newsFeedList.setAdapter(listAdapter);
					break;
				case UPDATE_LIST:
					listAdapter.refresh();
					newsFeedList.setSelectionFromTop(0, 0);
					break;
				default:
					break;
				}
				
				try {
					loadThread.join();
					initLoadThread();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				loadSection.setVisibility(View.GONE);
			}
		};
	}
	
	public void initLoadThread(){
		loadThread = new Thread(new Runnable() {	
			public void run() {
				Message message = loadHandler.obtainMessage(loadMode);
				provider.refresh();
				loadHandler.sendMessage(message);
			}
		});
	}
	
	private void initRefreshButton(){
		Button refreshButton = (Button) findViewById(R.id.refresh_button);
		refreshButton.setOnClickListener(new OnClickListener() {			
			public void onClick(View refreshButton) {
				loadMode = UPDATE_LIST;
				loadThread.start();
				loadSection.setVisibility(View.VISIBLE);
			}
		});
	}
	
	private void initWritePostButton(){
		Button writePostButton = (Button)findViewById(R.id.write_share_button);
		writePostButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(NewsFeedActivity.this, WritePostActivity.class);
				intent.putExtra(WritePostActivity.POST_MODE, WritePostActivity.FEED_MODE);
				startActivity(intent);
			}
		});
	}
	
	private void initNewsFeedList(){
		loadMode = INIT_LIST;
		loadThread.start();
		loadSection.setVisibility(View.VISIBLE);
	}
}

